package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckItemMapper checkItemMapper;
@Autowired
    CheckGroupMapper checkGroupMapper;
    @Override
    public void add(CheckGroup checkGroup) {
        //插入检查组信息并返回主键
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();
//        //循环用户勾选的检查项id集合(1,2,3)和检查组的的主键建立关系(5,1) (5,2)
//        if(CollectionUtil.isNotEmpty(checkitemIds)){
//            for (Integer checkitemId : checkitemIds) {
//                checkGroupMapper.setCheckItemAndCheckGroup(checkitemId,checkGroupId);
//               }
//        }
            setCheckItemAndCheckGroupBatch(checkitemIds,checkGroupId);
    }

    public void setCheckItemAndCheckGroupBatch(List<Integer> checkitemIds,Integer checkGroupId){
        if(CollectionUtil.isNotEmpty(checkitemIds)) {
            checkGroupMapper.setCheckItemAndCheckGroupBatch(checkitemIds, checkGroupId);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //告诉mybatis要分页
        com.github.pagehelper.Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroups=checkGroupMapper.findPage(queryPageBean.getQueryString());
        //上面两行位置不能换
        return new PageResult(page.getTotal(),checkGroups);
    }

    @Override
    public Map findById4Edit(Integer id) {
        Map map = new HashMap<>();
        //检查组的基本信息
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        //所有检查项
        List<CheckItem> checkItemList = checkItemMapper.findAll();
        //用户勾选的检查Id集合
        List<Integer> userIdList = checkGroupMapper.findUserId(id);
        map.put("checkGroup",checkGroup);
        map.put("checkItems",checkItemList);
        map.put("checkItemIds",userIdList);
        return map;
    }

    @Override
    public void edit(CheckGroup checkGroup) {
        //根据id修改检查组基本信息
        checkGroupMapper.edit(checkGroup);
        //删除原来信息
        checkGroupMapper.deleteAssociation(checkGroup.getId());
        //建立新的关系
        setCheckItemAndCheckGroupBatch(checkGroup.getCheckitemIds(),checkGroup.getId());


    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupMapper.findAll();
    }
}
